package me.zk.practice1.s;

/**
 * @author zhangkai
 * @date 2023/7/14 09:52
 */
public class LS_Offer_29 {
    public int[] spiralOrder(int[][] matrix) {
        if (matrix.length==0) {
            return new int[]{0};
        }
        int l = 0,r=matrix[0].length-1,t = 0,b=matrix.length-1,x=0;
        int[] res = new int[matrix[0].length * matrix.length];
        while (true) {
            for (int i = l; i <= r; i++) {
                res[x++] = matrix[t][i];
            }
            //防止超过底部
            if (++t>b) {
                break;
            }
            for (int i = t; i <= b; i++) {
                res[x++] = matrix[i][r];
            }
            if (--r < l) {
                break;
            }
            for (int i = r; i >=l ; i--) {
                res[x++] = matrix[b][i];
            }
            if (--b<t) {
                break;
            }
            for (int i = b; i >= t; i--) {
                res[x++] = matrix[i][l];
            }

            if (++l>r){
                break;
            }

        }

        return res;
    }

    public static void main(String[] args) {
        new LS_Offer_29().spiralOrder(new int[][]{{1,2,3,4},{5,6,7,8},{9,10,11,12}});
    }
}
